import javax.swing.*;
import java.awt.*;
import java.util.Arrays;

public class CardPanel extends JPanel{
	private CardClient main;
	private int cardWidth = 71;
	private int cardHeight = 96;
	private int cardSpaceX=15;  //显示两张牌时的水平间隔
	private int cardSpaceY=0;   //垂直间隔
	private int startx=0;     
	private int starty=0;
	private int type=0;
	private int position=1;
		
	public static final int BOTTOM=1;
	public static final int RIGHT=2;
	public static final int LEFT=3;
	public static final int BOTTOM_PLAY=4;
	public static final int RIGHT_PLAY=5;
	public static final int LEFT_PLAY=6;
	public static final int TOP=7;
	
	card[] pools;
    
    public CardPanel(CardClient main,int pos) {
    	super();
    	this.main=main;
    	this.position=pos;
    	pools=null;
    }
    
    public void paintComponent(Graphics g) {
		Graphics2D comp = (Graphics2D)g;
		changeShape();
		comp.setColor(main.getContentPane().getBackground());
		comp.fillRect(0,0,this.getSize().width,getSize().height);
		int x=startx;
		int y=starty;
		Toolkit tk = Toolkit.getDefaultToolkit();
		if(pools!=null){
			for(int i=pools.length-1;i>=0;i--) {
				Image img=tk.getImage("images/"+pools[i].code+".gif");
				if(pools[i].state) comp.drawImage(img,x,y-10,this);
				else comp.drawImage(img,x,y,this);
				x+=cardSpaceX;
				y+=cardSpaceY;
			}
		}
	}
	
	public void setCards(int [] cards){
		if(cards==null) {pools=null;return;}
		Arrays.sort(cards);
		pools=new card[cards.length];
		for(int i=0;i<cards.length;i++){
			pools[i]=new card(cards[i]);	
		}
		repaint();
	}
	
	public void refresh(){
		switch(position){
			case BOTTOM:		setCards(main.player.getCards());break;
			case RIGHT:			setCards(main.table.players[(main.player.seatNum+1)%3].getCards());break;
			case LEFT:			setCards(main.table.players[(main.player.seatNum+2)%3].getCards());break;
			case TOP:			setCards(main.table.lastCards);break;
			case BOTTOM_PLAY:	setCards(main.player.getDisCards());break;
			case RIGHT_PLAY:	setCards(main.table.players[(main.player.seatNum+1)%3].getDisCards());break;
			case LEFT_PLAY:		setCards(main.table.players[(main.player.seatNum+2)%3].getDisCards());break;
		}
		}
	
	public void clearCards(){
		pools=null;
		repaint();
	}	
	
	private class card{
		int code;
		boolean state=false;
		card(int code){
			this.code=code;
		}
		void changeState(){
		state=!state;
		System.out.println(state);
		}
	}
	
	public void setType(int type){
		this.type=type;
		switch(type){
			case 0:{
				cardSpaceX=15;
				cardSpaceY=0;
				startx=0;
				starty=20;
				break;
				}
			case 1:cardSpaceX=15;cardSpaceY=0;startx=0;break;
			case 2:cardSpaceX=0;cardSpaceY=5;startx=0;starty=0;break;
			}
	}
	
	public int clickCard(Point point){
		if(pools==null) return -1;
		int index=-1;
		int i;
		i=point.x-startx;
		if(i<0||i>((pools.length-1)*cardSpaceX+cardWidth)) return -1;
		else if(i>=(pools.length-1)*cardSpaceX&&i<=((pools.length-1)*cardSpaceX+cardWidth)) index=0;
		else{
			index=i/cardSpaceX;
			index=pools.length-index-1;
			}
		pools[index].changeState();
		repaint();
		return index;	
	}
	
	public void setCardSpaceX(int space){
		this.cardSpaceX=space;
	}
	
	public int [] getSelectedCards(){
		int n=0;
		int [] cards=new int[20];
		for(int i=0;i<pools.length;i++){
			if(pools[i].state) cards[n++]=pools[i].code;
			}
		if(n==0) return null;
		int [] cards2=new int[n];
		System.arraycopy(cards,0,cards2,0,n);
		java.util.Arrays.sort(cards2);
		return cards2;	
	}
	
	private void changeShape(){
		int n;
		if(pools==null) n=0;
		else n=pools.length;
		int w=(n-1)*cardSpaceX+cardWidth;
		int h=(n-1)*cardSpaceY+cardHeight;
		int x=getLocation().x;
		int y=getLocation().y;
		
		switch(position){
			case BOTTOM_PLAY:
			case BOTTOM:
				x=x+getSize().width/2-w/2;
				setBounds(x,y,w,getSize().height);break;
			case RIGHT:
			case LEFT:
				y=y+getSize().height/2-h/2;
				setBounds(x,y,getSize().width,h);break;
			case RIGHT_PLAY:{
				x=x+getSize().width-w;
				setBounds(x,y,w,getSize().height);break;
			}
			case LEFT_PLAY:{
				setBounds(x,y,w,getSize().height);break;
			}
	
		}		
	}
	
	public void selectCards(int []cards){
		if(cards==null||pools==null) {refresh();return;}
		refresh();
		int i=0,j=0;
		while(i<pools.length&&j<cards.length){
			if(pools[i].code==cards[j]) {
				pools[i].state=true;
				i++;
				j++;			
			}
			else i++;
		}
		repaint();
	}	
}